package com.bolt.auth.admin.api.sys.dto.converter;


import com.bolt.auth.admin.api.sys.dto.RoleDTO;
import com.bolt.auth.admin.api.sys.entity.RoleEntity;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2018/11/27.
 */
public class RoleConverter {
    private RoleConverter() {
// do nothing
    }

    /**
     * Entity对象转换为DTO对象
     *
     * @param role DO对象
     * @return DTO对象
     */
    public static RoleDTO toDTO(RoleEntity role) {
        if (Objects.isNull(role)) {
            return null;
        }
        RoleDTO result = new RoleDTO();
        result.setId(role.getId());
        result.setCode(role.getCode());
        result.setDescription(role.getDescription());
        result.setEnabled(role.getEnabled());
        result.setName(role.getName());
        result.setDataScope(role.getDataScope());
        result.setOrgs(OrgConverter.toDTO(role.getOrgs()));
        result.setVersionNO(role.getVersionNO());
        // TODO
        return result;
    }

    /**
     * DTO对象转换为Entity对象
     *
     * @param role DO对象
     * @return VO对象
     */
    public static RoleEntity toEntity(RoleDTO role) {
        if (Objects.isNull(role)) {
            return null;
        }
        RoleEntity result = new RoleEntity();

        result.setId(role.getId());
        result.setCode(role.getCode());
        result.setDescription(role.getDescription());
        result.setEnabled(role.getEnabled());
        result.setName(role.getName());
        result.setDataScope(role.getDataScope());
        result.setOrgs(OrgConverter.toEntity(role.getOrgs()));
        result.setVersionNO(role.getVersionNO());
        // TODO
        return result;
    }

    /**
     * DTO对象转换为DO对象
     *
     * @param role DO对象
     * @return VO对象
     */
    public static RoleEntity toEntity(RoleEntity result, RoleDTO role) {
        if (Objects.isNull(role)) {
            return null;
        }

        result.setId(role.getId());
        result.setCode(role.getCode());
        result.setDescription(role.getDescription());
        result.setEnabled(role.getEnabled());
        result.setName(role.getName());
        result.setDataScope(role.getDataScope());
        result.setOrgs(OrgConverter.toEntity(role.getOrgs()));
        result.setVersionNO(role.getVersionNO());
// TODO
        return result;
    }

    /**
     * 批量 Entity对象转换为DTO对象
     *
     * @param roleList Entity集合对象
     * @return DTO对象
     */
    public static List <RoleDTO> toDTO(List <RoleEntity> roleList) {
        if (Objects.isNull(roleList)) {
            return null;
        }
        return roleList.stream().map(RoleConverter::toDTO).collect(Collectors.toList());
    }

}
